import { Request, Response, NextFunction } from 'express';

export const logger = (req: Request, res: Response, next: NextFunction): void => {
  const start = Date.now();
  const { method, path } = req;

  res.on('finish', () => {
    const duration = Date.now() - start;
    const { statusCode } = res;
    const logLevel = statusCode >= 400 ? 'error' : 'info';
    
    const logMessage = `[${new Date().toISOString()}] ${method} ${path} ${statusCode} - ${duration}ms`;
    
    if (logLevel === 'error') {
      console.error(logMessage);
    } else {
      console.log(logMessage);
    }
  });

  next();
};
